import {
  dsx
} from '../../../tools/datax';

const _ = dsx.cmd

export const chatTool = {
  setup(sessionOpenId, userId) {
    // console.log(sessionOpenId, userId)
    return new Promise((resolve, reject) => {
      dsx.coll('chat')
        .where({
          sessionOpenId: _.eq(sessionOpenId)
        })
        .get()
        .then(res => {
          if (res.data.length == 0) {
            dsx.cloudUpdate('chat', null, {
                sessionOpenId,
                userId
              })
              .then(res => {
                resolve(res)
              })
              .catch(e => {
                reject(e)
              })
          } else {
            resolve(res)
          }
        })
        .catch(e => {
          reject(e)
        })
    })
  }
}